using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Buildings : CanTakeDamage {

    public int Shield = 100;
	public int CurrShield;
	private float mTimer = 0.0f;
	public float shieldRegenTime = 1.0f;
	public static bool beenHit = false;
	private float mHitTimer = 0.0f;
	public int regenFactor = 20;
	private GameObject Shields;
    public static bool refresh = false;

	public GUIStyle myCustomGUIStyle;
    public GUIStyle myCustomGUIStyle2;
    //Gui Variables
    public float[] GuiOffsetX;
    public float[] GuiOffsetY;
    public float[] GuiOffsetW; 
    public float[] GuiOffsetH;
	
	private GameObject cityhealthbar;
    private float initialShieldbar, initialHealthbar;
	private GameObject cityshieldbar;
	public GameObject cityPos;

	protected override void Start ()
	{
		cityhealthbar = GameObject.FindGameObjectWithTag("cityhealthbar");
		cityshieldbar = GameObject.FindGameObjectWithTag("cityshieldbar");
		cityPos = GameObject.FindGameObjectWithTag("cityPos");
		
		Shields = GameObject.FindGameObjectWithTag("Shields");
        Health += PlayerPrefs.GetInt("ResidentialBoost");
        initialShieldbar = cityshieldbar.guiTexture.pixelInset.width;
        initialHealthbar = cityhealthbar.guiTexture.pixelInset.width;
        base.Start ();
        FullShields();
        //gui stuff


	}
    void Refresh()
    {
        CurrHealth += PlayerPrefs.GetInt("ResidentialBoost");
        CurrShield +=PlayerPrefs.GetInt("ShieldBoost");
        Health += PlayerPrefs.GetInt("ResidentialBoost");
        Shield += PlayerPrefs.GetInt("ShieldBoost");
        refresh = false;
    }
    public void FullShields()
    {
        CurrShield = Shield;
		beenHit = false;
    }
	public void ShieldRegen()
	{
		if(CurrShield + regenFactor > Shield)
		{
			FullShields();
		}
		else
		CurrShield += regenFactor;
	}
	
	public void HitTimer()
	{
		if(beenHit)
		{
			mHitTimer+=Time.deltaTime;
		}
		if(mHitTimer > shieldRegenTime)
		{
			beenHit = false;
			mHitTimer = 0;
		}
	}
	public override void TakeDamage(int dmg)
	{ 
		beenHit = true;
		if((CurrShield - dmg) < 0)
		{
		  	int healthDamage = Mathf.Abs(CurrShield - dmg);
			CurrShield = 0;
			Shields.SetActive(false);
			base.TakeDamage(healthDamage);
		}
		else
		{
			CurrShield-=dmg;
		}
	}
	
	// Update is called once per frame
	void Update () 
    {

        cityshieldbar.guiTexture.pixelInset = new Rect(cityshieldbar.guiTexture.pixelInset.x, cityshieldbar.guiTexture.pixelInset.y, initialShieldbar*((float)CurrShield/Shield), cityshieldbar.guiTexture.pixelInset.height);
        cityhealthbar.guiTexture.pixelInset = new Rect(cityhealthbar.guiTexture.pixelInset.x, cityhealthbar.guiTexture.pixelInset.y, initialHealthbar * ((float)CurrHealth / Health), cityhealthbar.guiTexture.pixelInset.height);
        if (refresh)
        {
            Refresh();
        }
		HitTimer();
		if(!beenHit)
		{
			mTimer = 0.0f;
			Shields.SetActive(true);
			ShieldRegen();
		}
		else
		{
			mTimer += Time.deltaTime;
		}
	}
    void OnGUI()
    {
	//	Debug.Log(cityhealthbar + " " + cityhealthbar + " " + cityPos);

		
		//cityshieldbar.guiTexture.pixelInset = new Rect(cityshieldbar.guiTexture.pixelInset.x, cityshieldbar.guiTexture.pixelInset.y, cityshieldbar.guiTexture.pixelInset.width, cityhealthbar.guiTexture.pixelInset.width);
		//cityhealthbar.guiTexture.pixelInset = new Rect(cityhealthbar.guiTexture.pixelInset.x, cityhealthbar.guiTexture.pixelInset.y, cityhealthbar.guiTexture.pixelInset.width, cityhealthbar.guiTexture.pixelInset.width);

		//reloadBar.guiTexture.pixelInset = new Rect(reloadBar.guiTexture.pixelInset.x, reloadBar.guiTexture.pixelInset.y, reloadBar.guiTexture.pixelInset.width, (secondaryTimer > reloadMax) ? initialRbarHeight : initialRbarHeight*(secondaryTimer / reloadMax));
		
        //float rx = (float)UnityEngine.Screen.width / (float)Screen.width;
        //float ry = (float)UnityEngine.Screen.height / (float)Screen.height;
        ////Debug.Log(Mathf.Round(1.54f*10f)/10f);

        //rx = Mathf.Round(rx * 10f) / 10f;
        //rx = Mathf.Round(rx * 10f) / 10f;
        //Debug.Log("Rxdsfsdfdsf: " + GameGUI.rx + " Ry: " + GameGUI.ry + " " + GameGUI.Screen.width + " " + GameGUI.Screen.height);
        //Debug.Log(UnityEngine.Screen.width);
        //Debug.Log(UnityEngine.Screen.height);
        //Debug.Log(GameGUI.Screen.width + " " + GameGUI.Screen.height);
        //GUI.color = Color.blue;
        GUI.depth = 3;
        GUI.Label(new Rect(Screen.width /GuiOffsetX[0], Screen.height / GuiOffsetY[0], Screen.width/GuiOffsetW[0], Screen.height/GuiOffsetH[0]), CurrShield.ToString(),myCustomGUIStyle);
        //GUI.color = Color.green;
        
        GUI.Label(new Rect(Screen.width / GuiOffsetX[1], Screen.height / GuiOffsetY[1], Screen.width/GuiOffsetW[1], Screen.height/GuiOffsetH[1]), CurrHealth.ToString(), myCustomGUIStyle2);
    }

    public override void Die()
    {
        PlayerPrefs.SetInt("Currency", 0);
        Destroy(gameObject);
		Debug.Log ("Buildings died you lose + Health: "+ CurrHealth + " Shield:"+ CurrShield);
        Application.LoadLevel(3);
    }
}